﻿Public Class DockHelperGrid : Inherits Grid


    Dim innerGrid As Grid

    Dim OuterTopDockGizmo As Label
    Dim OuterLeftDockGizmo As Label
    Dim OuterBottomDockGizmo As Label
    Dim OuterRightDockGizmo As Label


    Dim InnerTopDockGizmo As Label
    Dim InnerLeftDockGizmo As Label
    Dim InnerBottomDockGizmo As Label
    Dim InnerRightDockGizmo As Label
    Dim InnerCenterDockGizmo As Label


    Dim PreviewGizmo As Label

    Dim CenterPoint As Point

    Dim MDG As MainDockGrid

    <Flags()>
    Enum VisibleGizmos

        OuterL = 1
        OuterT = 2
        OuterR = 4
        OuterB = 8


        InnerL = 16
        InnerT = 32
        InnerR = 64
        InnerB = 128
        InnerC = 256

        ' ALL = 512

    End Enum

    'if you are using a grid un comment this new sub
    Public Sub New(ByVal _MDG As MainDockGrid)

        MDG = _MDG


        OuterTopDockGizmo = New Label
        OuterTopDockGizmo.Width = 30
        OuterTopDockGizmo.Height = 30
        OuterTopDockGizmo.Background = Brushes.Yellow
        OuterTopDockGizmo.VerticalAlignment = Windows.VerticalAlignment.Top
        OuterTopDockGizmo.HorizontalAlignment = Windows.HorizontalAlignment.Center

        OuterLeftDockGizmo = New Label
        OuterLeftDockGizmo.Width = 30
        OuterLeftDockGizmo.Height = 30
        OuterLeftDockGizmo.Background = Brushes.Red
        OuterLeftDockGizmo.VerticalAlignment = Windows.VerticalAlignment.Center
        OuterLeftDockGizmo.HorizontalAlignment = Windows.HorizontalAlignment.Left


        OuterBottomDockGizmo = New Label
        OuterBottomDockGizmo.Width = 30
        OuterBottomDockGizmo.Height = 30
        OuterBottomDockGizmo.Background = Brushes.Green
        OuterBottomDockGizmo.VerticalAlignment = Windows.VerticalAlignment.Bottom
        OuterBottomDockGizmo.HorizontalAlignment = Windows.HorizontalAlignment.Center

        OuterRightDockGizmo = New Label
        OuterRightDockGizmo.Width = 30
        OuterRightDockGizmo.Height = 30
        OuterRightDockGizmo.Background = Brushes.Blue
        OuterRightDockGizmo.VerticalAlignment = Windows.VerticalAlignment.Center
        OuterRightDockGizmo.HorizontalAlignment = Windows.HorizontalAlignment.Right


        Children.Add(OuterTopDockGizmo)
        Children.Add(OuterLeftDockGizmo)
        Children.Add(OuterBottomDockGizmo)
        Children.Add(OuterRightDockGizmo)

        '^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v
        '^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v

        InnerTopDockGizmo = New Label
        InnerTopDockGizmo.Width = 25
        InnerTopDockGizmo.Height = 25
        InnerTopDockGizmo.Background = Brushes.Yellow
        InnerTopDockGizmo.VerticalAlignment = Windows.VerticalAlignment.Top
        InnerTopDockGizmo.HorizontalAlignment = Windows.HorizontalAlignment.Center

        InnerLeftDockGizmo = New Label
        InnerLeftDockGizmo.Width = 25
        InnerLeftDockGizmo.Height = 25
        InnerLeftDockGizmo.Background = Brushes.Red
        InnerLeftDockGizmo.VerticalAlignment = Windows.VerticalAlignment.Center
        InnerLeftDockGizmo.HorizontalAlignment = Windows.HorizontalAlignment.Left

        InnerBottomDockGizmo = New Label
        InnerBottomDockGizmo.Width = 25
        InnerBottomDockGizmo.Height = 25
        InnerBottomDockGizmo.Background = Brushes.Green
        InnerBottomDockGizmo.VerticalAlignment = Windows.VerticalAlignment.Bottom
        InnerBottomDockGizmo.HorizontalAlignment = Windows.HorizontalAlignment.Center

        InnerRightDockGizmo = New Label
        InnerRightDockGizmo.Width = 25
        InnerRightDockGizmo.Height = 25
        InnerRightDockGizmo.Background = Brushes.Blue
        InnerRightDockGizmo.VerticalAlignment = Windows.VerticalAlignment.Center
        InnerRightDockGizmo.HorizontalAlignment = Windows.HorizontalAlignment.Right

        InnerCenterDockGizmo = New Label
        InnerCenterDockGizmo.Width = 28
        InnerCenterDockGizmo.Height = 28
        InnerCenterDockGizmo.Background = Brushes.CornflowerBlue
        InnerCenterDockGizmo.VerticalAlignment = Windows.VerticalAlignment.Center
        InnerCenterDockGizmo.HorizontalAlignment = Windows.HorizontalAlignment.Center

        innerGrid = New Grid
        innerGrid.Width = 90
        innerGrid.Height = 90

        innerGrid.Children.Add(InnerTopDockGizmo)
        innerGrid.Children.Add(InnerLeftDockGizmo)
        innerGrid.Children.Add(InnerBottomDockGizmo)
        innerGrid.Children.Add(InnerRightDockGizmo)
        innerGrid.Children.Add(InnerCenterDockGizmo)


        innerGrid.HorizontalAlignment = Windows.HorizontalAlignment.Left
        innerGrid.VerticalAlignment = Windows.VerticalAlignment.Top

        Children.Add(innerGrid)

        innerGrid.Margin = New Thickness(60, 40, 0, 0)


        PreviewGizmo = New Label
        PreviewGizmo.Width = 1
        PreviewGizmo.Height = 1
        PreviewGizmo.Margin = New Thickness(0, 0, 0, 0)
        PreviewGizmo.VerticalAlignment = Windows.VerticalAlignment.Top
        PreviewGizmo.HorizontalAlignment = Windows.HorizontalAlignment.Left
        PreviewGizmo.Background = Brushes.LightGray
        PreviewGizmo.Opacity = 0.8R

        Children.Add(PreviewGizmo)
    End Sub

    Public Sub New()

        '     MDG = _MDG
        Background = Brushes.Transparent

        OuterTopDockGizmo = New Label
        OuterTopDockGizmo.Width = 30
        OuterTopDockGizmo.Height = 30
        OuterTopDockGizmo.Background = Brushes.Yellow
        OuterTopDockGizmo.VerticalAlignment = Windows.VerticalAlignment.Top
        OuterTopDockGizmo.HorizontalAlignment = Windows.HorizontalAlignment.Center


        OuterLeftDockGizmo = New Label
        OuterLeftDockGizmo.Width = 30
        OuterLeftDockGizmo.Height = 30
        OuterLeftDockGizmo.Background = Brushes.Red
        OuterLeftDockGizmo.VerticalAlignment = Windows.VerticalAlignment.Center
        OuterLeftDockGizmo.HorizontalAlignment = Windows.HorizontalAlignment.Left


        OuterBottomDockGizmo = New Label
        OuterBottomDockGizmo.Width = 30
        OuterBottomDockGizmo.Height = 30
        OuterBottomDockGizmo.Background = Brushes.Green
        OuterBottomDockGizmo.VerticalAlignment = Windows.VerticalAlignment.Bottom
        OuterBottomDockGizmo.HorizontalAlignment = Windows.HorizontalAlignment.Center

        OuterRightDockGizmo = New Label
        OuterRightDockGizmo.Width = 30
        OuterRightDockGizmo.Height = 30
        OuterRightDockGizmo.Background = Brushes.Blue
        OuterRightDockGizmo.VerticalAlignment = Windows.VerticalAlignment.Center
        OuterRightDockGizmo.HorizontalAlignment = Windows.HorizontalAlignment.Right

        Children.Add(OuterTopDockGizmo)
        Children.Add(OuterLeftDockGizmo)
        Children.Add(OuterBottomDockGizmo)
        Children.Add(OuterRightDockGizmo)

        '^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v
        '^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v

        InnerTopDockGizmo = New Label
        InnerTopDockGizmo.Width = 25
        InnerTopDockGizmo.Height = 25
        InnerTopDockGizmo.Background = Brushes.Yellow
        InnerTopDockGizmo.VerticalAlignment = Windows.VerticalAlignment.Top
        InnerTopDockGizmo.HorizontalAlignment = Windows.HorizontalAlignment.Center

        InnerLeftDockGizmo = New Label
        InnerLeftDockGizmo.Width = 25
        InnerLeftDockGizmo.Height = 25
        InnerLeftDockGizmo.Background = Brushes.Red
        InnerLeftDockGizmo.VerticalAlignment = Windows.VerticalAlignment.Center
        InnerLeftDockGizmo.HorizontalAlignment = Windows.HorizontalAlignment.Left
        InnerLeftDockGizmo.Visibility = Windows.Visibility.Collapsed

        InnerBottomDockGizmo = New Label
        InnerBottomDockGizmo.Width = 25
        InnerBottomDockGizmo.Height = 25
        InnerBottomDockGizmo.Background = Brushes.Green
        InnerBottomDockGizmo.VerticalAlignment = Windows.VerticalAlignment.Bottom
        InnerBottomDockGizmo.HorizontalAlignment = Windows.HorizontalAlignment.Center

        InnerRightDockGizmo = New Label
        InnerRightDockGizmo.Width = 25
        InnerRightDockGizmo.Height = 25
        InnerRightDockGizmo.Background = Brushes.Blue
        InnerRightDockGizmo.VerticalAlignment = Windows.VerticalAlignment.Center
        InnerRightDockGizmo.HorizontalAlignment = Windows.HorizontalAlignment.Right

        InnerCenterDockGizmo = New Label
        InnerCenterDockGizmo.Width = 28
        InnerCenterDockGizmo.Height = 28
        InnerCenterDockGizmo.Background = Brushes.CornflowerBlue
        InnerCenterDockGizmo.VerticalAlignment = Windows.VerticalAlignment.Center
        InnerCenterDockGizmo.HorizontalAlignment = Windows.HorizontalAlignment.Center

        innerGrid = New Grid
        innerGrid.Width = 90
        innerGrid.Height = 90

        innerGrid.Children.Add(InnerTopDockGizmo)
        innerGrid.Children.Add(InnerLeftDockGizmo)
        innerGrid.Children.Add(InnerBottomDockGizmo)
        innerGrid.Children.Add(InnerRightDockGizmo)
        innerGrid.Children.Add(InnerCenterDockGizmo)


        innerGrid.HorizontalAlignment = Windows.HorizontalAlignment.Left
        innerGrid.VerticalAlignment = Windows.VerticalAlignment.Top

        Children.Add(innerGrid)

        innerGrid.Margin = New Thickness(60, 40, 0, 0)

    End Sub


    'Public Function ProcessDockingPointRequest(ByVal pToScreen As Point, ByRef targetGrid As Grid) As MainDockGrid.DockPosition

    '    Dim pt As Point = PointFromScreen(pToScreen)

    '    '     If Not (pt.X >= 0 And pt.X < ActualWidth) Then Return Nothing
    '    '    If Not (pt.Y >= 0 And pt.Y < ActualHeight) Then Return Nothing
    '    Dim dckPos As MainDockGrid.DockPosition = MainDockGrid.DockPosition.Left
    '    Dim g As FrameworkElement
    '    UpdatePreviewGizmo(0, 0, 1, 1)
    '    For x = 0 To Children.Count - 1
    '        pt = Children(x).PointFromScreen(pToScreen)
    '        g = Children(x)
    '        If pt.X >= 0 And pt.X < g.ActualWidth Then
    '            If pt.Y >= 0 And pt.Y < g.ActualHeight Then

    '                If TypeOf (g) Is Label Then
    '                    targetGrid = MDG.GetVeryBaseGrid()
    '                    If g Is OuterBottomDockGizmo Then dckPos = MainDockGrid.DockPosition.Bottom : UpdatePreviewGizmo(0, ActualHeight / 2, ActualWidth, ActualHeight / 2)
    '                    If g Is OuterLeftDockGizmo Then dckPos = MainDockGrid.DockPosition.Left : UpdatePreviewGizmo(0, 0, ActualWidth / 2, ActualHeight)
    '                    If g Is OuterTopDockGizmo Then dckPos = MainDockGrid.DockPosition.Top : UpdatePreviewGizmo(0, 0, ActualWidth, ActualHeight / 2)
    '                    If g Is OuterRightDockGizmo Then dckPos = MainDockGrid.DockPosition.Right : UpdatePreviewGizmo(ActualWidth / 2, 0, ActualWidth / 2, ActualHeight)

    '                    Return dckPos
    '                End If

    '                If g Is innerGrid Then
    '                    'g = g.InputHitTest(PointFromScreen(pToScreen))
    '                    For z = 0 To innerGrid.Children.Count - 1
    '                        pt = innerGrid.Children(z).PointFromScreen(pToScreen)
    '                        g = innerGrid.Children(z)
    '                        If pt.X >= 0 And pt.X < g.ActualWidth Then
    '                            If pt.Y >= 0 And pt.Y < g.ActualHeight Then
    '                                targetGrid = lastProbedGrid         'Mi aspetto che questo valore non sia nullo.
    '                                If g Is InnerBottomDockGizmo Then dckPos = MainDockGrid.DockPosition.Bottom
    '                                If g Is InnerLeftDockGizmo Then dckPos = MainDockGrid.DockPosition.Left
    '                                If g Is InnerTopDockGizmo Then dckPos = MainDockGrid.DockPosition.Top
    '                                If g Is InnerRightDockGizmo Then dckPos = MainDockGrid.DockPosition.Right

    '                                Return dckPos
    '                            End If
    '                        End If
    '                    Next
    '                End If

    '                Return Nothing
    '            End If
    '        End If
    '    Next

    '    '  Dim g As FrameworkElement = InputHitTest(PointFromScreen(pToScreen))


    '    Return Nothing
    'End Function

    Dim FLAG_ShowDockPreview As Boolean = True
    Public Function OLD_ProcessDockingPointRequest(ByVal pToScreen As Point, ByRef targetGrid As Grid) As MainDockGrid.DockPosition

        Dim pt As Point = PointFromScreen(pToScreen)

        '     If Not (pt.X >= 0 And pt.X < ActualWidth) Then Return Nothing
        '    If Not (pt.Y >= 0 And pt.Y < ActualHeight) Then Return Nothing
        Dim dckPos As MainDockGrid.DockPosition = MainDockGrid.DockPosition.Left
        Dim g As FrameworkElement
        Dim lt As Point
        Dim rb As Point
        UpdatePreviewGizmo(0, 0, 1, 1)
        For x = 0 To Children.Count - 1
            pt = Children(x).PointFromScreen(pToScreen)
            g = Children(x)
            If pt.X >= 0 And pt.X < g.ActualWidth Then
                If pt.Y >= 0 And pt.Y < g.ActualHeight Then

                    If TypeOf (g) Is Label Then
                        targetGrid = MDG.GetVeryBaseGrid()
                        If FLAG_ShowDockPreview Then

                            lt = PointToVisual(MDG, targetGrid.PointToScreen(New Point(0, 0)))
                            rb = PointToVisual(MDG, targetGrid.PointToScreen(New Point(targetGrid.ActualWidth, targetGrid.ActualHeight)))

                            If g Is OuterBottomDockGizmo Then dckPos = MainDockGrid.DockPosition.Bottom : UpdatePreviewGizmo(lt.X, lt.Y + targetGrid.ActualHeight / 2, targetGrid.ActualWidth, targetGrid.ActualHeight / 2)
                            If g Is OuterLeftDockGizmo Then dckPos = MainDockGrid.DockPosition.Left : UpdatePreviewGizmo(lt.X, lt.Y, targetGrid.ActualWidth / 2, targetGrid.ActualHeight)
                            If g Is OuterTopDockGizmo Then dckPos = MainDockGrid.DockPosition.Top : UpdatePreviewGizmo(lt.X, lt.Y, targetGrid.ActualWidth, targetGrid.ActualHeight / 2)
                            If g Is OuterRightDockGizmo Then dckPos = MainDockGrid.DockPosition.Right : UpdatePreviewGizmo(lt.X + targetGrid.ActualWidth / 2, lt.Y, targetGrid.ActualWidth / 2, targetGrid.ActualHeight)
                        Else
                            If g Is OuterBottomDockGizmo Then dckPos = MainDockGrid.DockPosition.Bottom
                            If g Is OuterLeftDockGizmo Then dckPos = MainDockGrid.DockPosition.Left
                            If g Is OuterTopDockGizmo Then dckPos = MainDockGrid.DockPosition.Top
                            If g Is OuterRightDockGizmo Then dckPos = MainDockGrid.DockPosition.Right
                        End If
                        Return dckPos
                    End If

                    If g Is innerGrid Then
                        'g = g.InputHitTest(PointFromScreen(pToScreen))
                        For z = 0 To innerGrid.Children.Count - 1
                            pt = innerGrid.Children(z).PointFromScreen(pToScreen)
                            g = innerGrid.Children(z)
                            If pt.X >= 0 And pt.X < g.ActualWidth Then
                                If pt.Y >= 0 And pt.Y < g.ActualHeight Then
                                    targetGrid = lastProbedGrid         'Mi aspetto che questo valore non sia nullo.
                                    If FLAG_ShowDockPreview Then

                                        lt = PointToVisual(MDG, targetGrid.PointToScreen(New Point(0, 0)))
                                        rb = PointToVisual(MDG, targetGrid.PointToScreen(New Point(targetGrid.ActualWidth, targetGrid.ActualHeight)))

                                        If g Is InnerBottomDockGizmo Then dckPos = MainDockGrid.DockPosition.Bottom : UpdatePreviewGizmo(lt.X, lt.Y + targetGrid.ActualHeight / 2, targetGrid.ActualWidth, targetGrid.ActualHeight / 2)
                                        If g Is InnerLeftDockGizmo Then dckPos = MainDockGrid.DockPosition.Left : UpdatePreviewGizmo(lt.X, lt.Y, targetGrid.ActualWidth / 2, targetGrid.ActualHeight)
                                        If g Is InnerTopDockGizmo Then dckPos = MainDockGrid.DockPosition.Top : UpdatePreviewGizmo(lt.X, lt.Y, targetGrid.ActualWidth, targetGrid.ActualHeight / 2)
                                        If g Is InnerRightDockGizmo Then dckPos = MainDockGrid.DockPosition.Right : UpdatePreviewGizmo(lt.X + targetGrid.ActualWidth / 2, lt.Y, targetGrid.ActualWidth / 2, targetGrid.ActualHeight)
                                        If g Is InnerCenterDockGizmo Then dckPos = MainDockGrid.DockPosition.Center : UpdatePreviewGizmo(lt.X, lt.Y, targetGrid.ActualWidth, targetGrid.ActualHeight)

                                    Else
                                        If g Is InnerBottomDockGizmo Then dckPos = MainDockGrid.DockPosition.Bottom
                                        If g Is InnerLeftDockGizmo Then dckPos = MainDockGrid.DockPosition.Left
                                        If g Is InnerTopDockGizmo Then dckPos = MainDockGrid.DockPosition.Top
                                        If g Is InnerRightDockGizmo Then dckPos = MainDockGrid.DockPosition.Right
                                        If g Is InnerCenterDockGizmo Then dckPos = MainDockGrid.DockPosition.Center

                                    End If
                                    Return dckPos
                                End If
                            End If
                        Next
                    End If

                    Return Nothing
                End If
            End If
        Next

        '  Dim g As FrameworkElement = InputHitTest(PointFromScreen(pToScreen))


        Return Nothing
    End Function

    Public Function ProcessDockingPointRequest(ByVal pToScreen As Point, ByRef targetGrid As Grid) As MainDockGrid.DockPosition

        'I controlli servono ad impedire chiamate inutili a questa istruzione.

        '  Visibility = Windows.Visibility.Hidden

        'Controllo #1
        If Not FECPTS(Me, pToScreen) Then
            Visibility = Windows.Visibility.Hidden
            Return Nothing
        End If

        'ci può essere il caso per cui MainGrid non contenga alcun grid. In questo caso si reputa necessario, imporre che il grid che si intende dockare diventi il contenuto di MainGrid.

        If MDG.MainGrid.Children.Count = 0 And FECPTS(MDG.MainGrid, pToScreen) Then
            MoveInnerGizmo(MDG.MainGrid)
            targetGrid = MDG.MainGrid
            Visibility = Windows.Visibility.Visible
            UpdatePreviewGizmo(0, 0, 1, 1) ' azzera il PreviewGizmo.

            If FLAG_ShowDockPreview And FECPTS(InnerCenterDockGizmo, pToScreen) Then
                '  Visibility = Windows.Visibility.Visible
                Dim lt As Point
                Dim rb As Point

                lt = PointToVisual(MDG, MDG.MainGrid.PointToScreen(New Point(0, 0)))
                rb = PointToVisual(MDG, MDG.MainGrid.PointToScreen(New Point(MDG.MainGrid.ActualWidth, MDG.MainGrid.ActualHeight)))
                UpdatePreviewGizmo(lt.X, lt.Y, MDG.MainGrid.ActualWidth, MDG.MainGrid.ActualHeight)
            End If

            Return MainDockGrid.DockPosition.Center

        End If

        If FLAG_ShowDockPreview Then
            UpdatePreviewGizmo(0, 0, 1, 1) ' azzera il PreviewGizmo.
            Dim lt As Point
            Dim rb As Point
            targetGrid = MDG.GetVeryBaseGrid

            lt = PointToVisual(MDG, targetGrid.PointToScreen(New Point(0, 0)))
            rb = PointToVisual(MDG, targetGrid.PointToScreen(New Point(targetGrid.ActualWidth, targetGrid.ActualHeight)))

            Visibility = Windows.Visibility.Visible

            If FECPTS(OuterBottomDockGizmo, pToScreen) Then
                UpdatePreviewGizmo(lt.X, lt.Y + targetGrid.ActualHeight / 2, targetGrid.ActualWidth, targetGrid.ActualHeight / 2)
                Return MainDockGrid.DockPosition.Bottom
            End If

            If FECPTS(OuterRightDockGizmo, pToScreen) Then
                UpdatePreviewGizmo(lt.X + targetGrid.ActualWidth / 2, lt.Y, targetGrid.ActualWidth / 2, targetGrid.ActualHeight)
                Return MainDockGrid.DockPosition.Right
            End If
            If FECPTS(OuterLeftDockGizmo, pToScreen) Then
                UpdatePreviewGizmo(lt.X, lt.Y, targetGrid.ActualWidth / 2, targetGrid.ActualHeight)
                Return MainDockGrid.DockPosition.Left
            End If

            If FECPTS(OuterTopDockGizmo, pToScreen) Then
                UpdatePreviewGizmo(lt.X, lt.Y, targetGrid.ActualWidth, targetGrid.ActualHeight / 2)
                Return MainDockGrid.DockPosition.Top
            End If


            targetGrid = lastProbedGrid

            lt = PointToVisual(MDG, targetGrid.PointToScreen(New Point(0, 0)))
            rb = PointToVisual(MDG, targetGrid.PointToScreen(New Point(targetGrid.ActualWidth, targetGrid.ActualHeight)))


            If FECPTS(InnerBottomDockGizmo, pToScreen) Then
                UpdatePreviewGizmo(lt.X, lt.Y + targetGrid.ActualHeight / 2, targetGrid.ActualWidth, targetGrid.ActualHeight / 2)
                Return MainDockGrid.DockPosition.Bottom
            End If

            If FECPTS(InnerRightDockGizmo, pToScreen) Then
                UpdatePreviewGizmo(lt.X + targetGrid.ActualWidth / 2, lt.Y, targetGrid.ActualWidth / 2, targetGrid.ActualHeight)
                Return MainDockGrid.DockPosition.Right
            End If

            If FECPTS(InnerLeftDockGizmo, pToScreen) Then
                UpdatePreviewGizmo(lt.X, lt.Y, targetGrid.ActualWidth / 2, targetGrid.ActualHeight)
                Return MainDockGrid.DockPosition.Left
            End If

            If FECPTS(InnerTopDockGizmo, pToScreen) Then
                UpdatePreviewGizmo(lt.X, lt.Y, targetGrid.ActualWidth, targetGrid.ActualHeight / 2)
                Return MainDockGrid.DockPosition.Top
            End If

            If FECPTS(InnerCenterDockGizmo, pToScreen) Then
                UpdatePreviewGizmo(lt.X, lt.Y, targetGrid.ActualWidth, targetGrid.ActualHeight)
                Return MainDockGrid.DockPosition.Center
            End If



        Else

            targetGrid = MDG.GetVeryBaseGrid

            If FECPTS(OuterBottomDockGizmo, pToScreen) Then Return MainDockGrid.DockPosition.Bottom
            If FECPTS(OuterRightDockGizmo, pToScreen) Then Return MainDockGrid.DockPosition.Right
            If FECPTS(OuterLeftDockGizmo, pToScreen) Then Return MainDockGrid.DockPosition.Left
            If FECPTS(OuterTopDockGizmo, pToScreen) Then Return MainDockGrid.DockPosition.Top


            targetGrid = lastProbedGrid
            If FECPTS(InnerBottomDockGizmo, pToScreen) Then Return MainDockGrid.DockPosition.Bottom
            If FECPTS(InnerLeftDockGizmo, pToScreen) Then Return MainDockGrid.DockPosition.Left
            If FECPTS(InnerRightDockGizmo, pToScreen) Then Return MainDockGrid.DockPosition.Right
            If FECPTS(InnerTopDockGizmo, pToScreen) Then Return MainDockGrid.DockPosition.Top
            If FECPTS(InnerCenterDockGizmo, pToScreen) Then Return MainDockGrid.DockPosition.Center

        End If

        Return MainDockGrid.DockPosition.Null

    End Function



    Public Function PointToVisual(ByRef frel As FrameworkElement, ByVal pToScreen As Point)
        Return frel.PointFromScreen(pToScreen)
    End Function

    Public Function FECPTS(ByRef frel As FrameworkElement, ByVal pToScreen As Point) As Boolean 'FrameworkElementContainsPointToScreen

        Dim lt As Point = frel.PointToScreen(New Point(0, 0))
        Dim rb As Point = frel.PointToScreen(New Point(frel.ActualWidth, frel.ActualHeight))

        '   Return New Rect(lt, rb).Contains(pToScreen)
        Return lt.X < pToScreen.X And pToScreen.X < rb.X And lt.Y < pToScreen.Y And pToScreen.Y < rb.Y

    End Function


    'Public Function ProcessDockingPointRequest_EX(ByVal pToScreen As Point, ByRef targetGrid As Grid) As MainDockGrid.DockPosition

    '    Dim pt As Point = PointFromScreen(pToScreen)

    '    '     If Not (pt.X >= 0 And pt.X < ActualWidth) Then Return Nothing
    '    '    If Not (pt.Y >= 0 And pt.Y < ActualHeight) Then Return Nothing
    '    Dim g As FrameworkElement

    '    Dim lt As Point
    '    Dim rb As Point
    '    Dim returnedDockPos As MainDockGrid.DockPosition
    '    For x = 0 To Children.Count - 1
    '        pt = Children(x).PointFromScreen(pToScreen)
    '        g = Children(x)
    '        If pt.X >= 0 And pt.X < g.ActualWidth Then
    '            If pt.Y >= 0 And pt.Y < g.ActualHeight Then

    '                If TypeOf (g) Is Label Then
    '                    lt = PointFromScreen(g.PointToScreen(New Point(0, 0)))
    '                    '  rb = PointFromScreen(g.PointToScreen(New Point(g.ActualWidth, g.ActualHeight)))
    '                    targetGrid = MDG.GetVeryBaseGrid()
    '                    If g Is OuterBottomDockGizmo Then

    '                        Return MainDockGrid.DockPosition.Bottom
    '                    End If

    '                    If g Is OuterLeftDockGizmo Then

    '                        Return MainDockGrid.DockPosition.Left
    '                    End If

    '                    If g Is OuterTopDockGizmo Then

    '                        Return MainDockGrid.DockPosition.Top
    '                    End If

    '                    If g Is OuterRightDockGizmo Then

    '                        Return MainDockGrid.DockPosition.Right
    '                    End If



    '                End If

    '                If g Is innerGrid Then
    '                    'g = g.InputHitTest(PointFromScreen(pToScreen))
    '                    For z = 0 To innerGrid.Children.Count - 1
    '                        pt = innerGrid.Children(z).PointFromScreen(pToScreen)
    '                        g = innerGrid.Children(z)
    '                        If pt.X >= 0 And pt.X < g.ActualWidth Then
    '                            If pt.Y >= 0 And pt.Y < g.ActualHeight Then
    '                                targetGrid = lastProbedGrid         'Mi aspetto che questo valore non sia nullo.
    '                                If g Is InnerBottomDockGizmo Then Return MainDockGrid.DockPosition.Bottom
    '                                If g Is InnerLeftDockGizmo Then Return MainDockGrid.DockPosition.Left
    '                                If g Is InnerTopDockGizmo Then Return MainDockGrid.DockPosition.Top
    '                                If g Is InnerRightDockGizmo Then Return MainDockGrid.DockPosition.Right

    '                            End If
    '                        End If
    '                    Next
    '                End If

    '                Return Nothing
    '            End If
    '        End If
    '    Next

    '    '  Dim g As FrameworkElement = InputHitTest(PointFromScreen(pToScreen))


    '    Return Nothing
    'End Function

    Dim lastProbedGrid As Grid
    Public Sub MoveInnerGizmo(ByRef probeGrid As Grid)
        'If lastProbedGrid Is probeGrid Then Return 'If lastProbedGrid Is Nothing Then
        '    lastProbedGrid = probeGrid
        'Else
        '    If lastProbedGrid Is probeGrid Then Return 'è inutile spostare di nuovo il grid se la posizione che dovrà assumere è uguale alla precedente.
        'End If

        Dim LT As Point = probeGrid.PointToScreen(New Point(0, 0))
        Dim RB As Point = probeGrid.PointToScreen(New Point(probeGrid.ActualWidth, probeGrid.ActualHeight))

        ' Dim probeGridMirroredRect As Rect = New Rect(LT, RB)
        Dim _newCenter As Point = PointFromScreen(New Point((LT.X + RB.X) / 2, (LT.Y + RB.Y) / 2))

        innerGrid.Margin = New Thickness(_newCenter.X - innerGrid.ActualWidth / 2, _newCenter.Y - innerGrid.ActualHeight / 2, 0, 0)
        lastProbedGrid = probeGrid
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="locX"></param>
    ''' <param name="locY"></param>
    ''' <param name="w"></param>
    ''' <param name="h"></param>
    ''' <remarks>locX e locY sono PointsToControls Dimensions</remarks>
    Public Sub UpdatePreviewGizmo(ByVal locX As Double, ByVal locY As Double, ByVal w As Double, ByVal h As Double)
        PreviewGizmo.Margin = New Thickness(locX, locY, 0, 0)
        PreviewGizmo.Width = w
        PreviewGizmo.Height = h
        Panel.SetZIndex(PreviewGizmo, -1)       'Assicura che PreviewGizmo venga disegnato in background.
    End Sub

    Public Sub Mask(ByVal gizmos As DockHelperGrid.VisibleGizmos)
        _mask()
    End Sub

    Private Sub _mask()
        OuterTopDockGizmo.Visibility = Windows.Visibility.Collapsed
        OuterRightDockGizmo.Visibility = Windows.Visibility.Collapsed
        OuterLeftDockGizmo.Visibility = Windows.Visibility.Collapsed
        OuterBottomDockGizmo.Visibility = Windows.Visibility.Collapsed

        InnerTopDockGizmo.Visibility = Windows.Visibility.Collapsed
        InnerRightDockGizmo.Visibility = Windows.Visibility.Collapsed
        InnerBottomDockGizmo.Visibility = Windows.Visibility.Collapsed
        InnerLeftDockGizmo.Visibility = Windows.Visibility.Collapsed
    End Sub

    Public Sub UnMask(ByVal gizmos As DockHelperGrid.VisibleGizmos)
        _mask()

        If gizmos.HasFlag(VisibleGizmos.OuterT) Then
            OuterTopDockGizmo.Visibility = Windows.Visibility.Visible
        End If
        If gizmos.HasFlag(VisibleGizmos.OuterR) Then
            OuterRightDockGizmo.Visibility = Windows.Visibility.Visible
        End If
        If gizmos.HasFlag(VisibleGizmos.OuterB) Then
            OuterBottomDockGizmo.Visibility = Windows.Visibility.Visible
        End If
        If gizmos.HasFlag(VisibleGizmos.OuterL) Then
            OuterLeftDockGizmo.Visibility = Windows.Visibility.Visible
        End If
        If gizmos.HasFlag(VisibleGizmos.InnerT) Then
            InnerTopDockGizmo.Visibility = Windows.Visibility.Visible
        End If
        If gizmos.HasFlag(VisibleGizmos.InnerR) Then
            InnerRightDockGizmo.Visibility = Windows.Visibility.Visible
        End If
        If gizmos.HasFlag(VisibleGizmos.InnerB) Then
            InnerBottomDockGizmo.Visibility = Windows.Visibility.Visible
        End If
        If gizmos.HasFlag(VisibleGizmos.InnerL) Then
            InnerLeftDockGizmo.Visibility = Windows.Visibility.Visible
        End If
        If gizmos.HasFlag(VisibleGizmos.InnerC) Then
            InnerCenterDockGizmo.Visibility = Windows.Visibility.Visible
        End If

    End Sub


    Public Sub Dispose()
        MDG = Nothing
        innerGrid = Nothing
        PreviewGizmo = Nothing
    End Sub

End Class
